<?php


class m_order_barang extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function get_total_pembelian($where = null, $limit = null, $offset = null)
    {
        $this->db->select('sum(TOTAL) as total');
        if($where != null) $this->db->where($where);
        if($limit != null && $offset != null) $this->db->limit($limit, $offset);
        return $this->db->get("order_barang");
    }

    function get($where = null, $limit = null) {
        if ($where != null)
            $this->db->where($where);
        return $this->db->get('order_barang');
    }

    function get_non($where = null, $limit = null) {
        $this->db->where('ID_FAKTUR', 0);
        return $this->db->get('order_barang');
    }

    function get_id($nomer_order) {
        $this->db->where('NOMER_ORDER', $nomer_order);
        return $this->db->get('order_barang');
    }

    function delete_order($id_order){
        $this->db->where('ID_ORDER',$id_order);
        $this->db->delete('detail_order');
        $this->db->where('ID_ORDER',$id_order);
        return $this->db->delete('order_barang');
    }

    function insert() {           

        $data = array(
            'ID_TOKO' => $this->input->post('ID_TOKO', TRUE),
            'NOMER_ORDER' => $this->input->post('NOMER_ORDER', TRUE),
            'ID_SUPLIER' => $this->input->post('SUPLIER',TRUE),
            'TGL_ORDER' => $this->input->post('TGL_ORDER', TRUE),
        );

        $this->db->insert('order_barang', $data);
        return $this->db->insert_id();
    }

    function update($id_order) {

        $data = array(
            'NOMER_ORDER' => $this->input->post('NOMER_ORDER', TRUE),
            'TGL_ORDER' => $this->input->post('TGL_ORDER', TRUE),
            //'DISKON' => $this->input->post('DISKON', TRUE),
            //'PPN' => $this->input->post('PPN', TRUE),
            'TOTAL' => $this->input->post('TOTAL', TRUE),
        );

        $this->db->where('ID_ORDER', $id_order);

        return $this->db->update('order_barang', $data);
    }

    function realisasi($id_order,$id_faktur) {

        $data = array(
            'ID_FAKTUR' => $id_faktur
        );

        $this->db->where('ID_ORDER', $id_order);

        return $this->db->update('order_barang', $data);
    }

    function getNomerOrder(){
        $this->db->select("MAX(ID_ORDER) as ID_ORDER");
        return $this->db->get('order_barang');
    }

    function getInfoOrder($idorder){
        $query = "SELECT ob.NOMER_ORDER, ob.TGL_ORDER, sp.NAMA_SUPLIER, sp.KOTA, sp.ALAMAT, sp.TELPON1
                    FROM  `order_barang` ob, suplier sp
                    WHERE ob.id_suplier = sp.kode_suplier
                    and ob.id_order = $idorder
                    ";
        return $this->db->query($query);
        
    }

}
?>
